﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffDesktop
{
    /*
     * Class: SendOff
     * 
     * Use: Form used to edit details of a Sending Off within
     *      a Match
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class SendOff : Form
    {
        //The Match in which the Sending Off took place
        public Match referrer;


        //Details required to connect to the database
        SqlCeConnection conn;
        String conString;
        String desktop_connection = @"Data Source=RefOff_DB.sdf";
        
        //Details of the Sending Off incident
        int sSendOff_id;
        String sSendOffCode;
        int sPlayer_id;
        String sPlayerName;
        int sMatch_id;
        String sClub;
        String sTime;
        String sWhistleGPS = "";
        String sIncidentGPS = "";

        //List of all available disciplinary codes
        public List<String> sendOffCodes;
        
        public SendOff()
        {
            InitializeComponent();
            referrer = null;
            
            //Add disciplinary codes to array
            sendOffCodes = new List<String>();
            sendOffCodes.Add("S1");
            sendOffCodes.Add("S2");
            sendOffCodes.Add("S3");
            sendOffCodes.Add("S4");
            sendOffCodes.Add("S5");
            sendOffCodes.Add("S6");
            sendOffCodes.Add("S7");
            
            //Add disciplinary codes to relevant combo box
            addSendOffCodes();
        }

        //Method to add disciplinary codes to relevant combo box when
        //form is initially created
        private void addSendOffCodes()
        {
            for (int i = 0; i < sendOffCodes.Count; i++)
            {
                sendOffSendOffCodeCombo.Items.Add(sendOffCodes[i]);
            }
        }

        //Method to select the correct value from the disciplinary code
        //combo box when the form is loaded or closed
        public void initCode()
        {
            int index = 0;
            int counter = 0;

            //Go through each item in the combo box
            for (int i = 0; i < sendOffSendOffCodeCombo.Items.Count; i++)
            {
                //If it matches the disciplinary code of this SendingOff..
                if (sendOffSendOffCodeCombo.Items[i].ToString().Equals(sSendOffCode))
                {
                    //Make a note of the items index in the combo box
                    index = counter;
                }
                counter++;
            }

            //Set the item as the default selected item in the combo box
            sendOffSendOffCodeCombo.SelectedIndex = index;
        }

        //Setter and getter for database id of this Sending Off
        public int sendoff_id
        {
            get
            {
                return sSendOff_id;
            }

            set
            {
                sSendOff_id = value;
            }
        }

        //Setter and getter for the disciplinary code of this Sending Off
        public String sendoff_code
        {
            get
            {
                return sSendOffCode;
            }

            set
            {
                sSendOffCode = value;
            }
        }

        //Setter and getter for the database id of the player who got sent off
        public int player_id
        {
            get
            {
                return sPlayer_id;
            }

            set
            {
                sPlayer_id = value;
            }
        }

        //Setter and getter for full name of player who got sent off (number, firstname, surname)
        public String player_name
        {
            get
            {
                return sPlayerName;
            }

            set
            {
                sPlayerName = value;
            }
        }

        //Setter and getter for database id of match the sending off took place in
        public int match_id
        {
            get
            {
                return sMatch_id;
            }

            set
            {
                sMatch_id = value;
            }
        }

        //Setter and getter for name of club awarded Sending Off
        public String club
        {
            get
            {
                return sClub;
            }

            set
            {
                sClub = value;
            }
        }

        //Setter and getter for time the Sending Off happened at
        public String time
        {
            get
            {
                return sTime;
            }

            set
            {
                sTime = value;
            }
        }

        //Setter and getter for GPS location of whistle event for this Sending Off
        public String whistleGPS
        {
            get
            {
                return sWhistleGPS;
            }

            set
            {
                sWhistleGPS = value;
            }
        }

        //Setter and getter for GPS location of Sending Off incident
        public String incidentGPS
        {
            get
            {
                return sIncidentGPS;
            }

            set
            {
                sIncidentGPS = value;
            }
        }

        //Method called when the 'Save' button is clicked
        private void sendOffSaveBtn_Click(object sender, EventArgs e)
        {
            //Get hold of the index and value of the selected disciplinary code
            int index = sendOffSendOffCodeCombo.SelectedIndex;
            sSendOffCode = sendOffSendOffCodeCombo.Items[index].ToString();

            //Connect to the database
            connectToDatabase(desktop_connection);

            //Create and execute SQL command to update disciplinary code of
            //Sending Off in database
            string sendoff_update_command = @"UPDATE sendoff SET sendoff_code = @code WHERE sendoff_id= " + sSendOff_id + ";";
            using (SqlCeCommand sendoff_update = new SqlCeCommand(sendoff_update_command, conn))
            {
                sendoff_update.Parameters.AddWithValue("@code", sSendOffCode);
                sendoff_update.ExecuteNonQuery();
            }

            //Disconnect from database
            closeDatabaseConnection();

            //Hide this form and display Match Details screen
            referrer.Enabled = true;
            this.Visible = false;

            //Set new default value of combo box
            initCode();

            //Update text in Match Details screen to reflect changes
            referrer.matchSendoffLst.SelectedItems[0].SubItems[2].Text = sSendOffCode;
        }

        //Method called when 'Close' button is clicked
        private void sendOffCloseBtn_Click(object sender, EventArgs e)
        {
            //Hide this form and display Match Details screen
            referrer.Enabled = true;
            this.Visible = false;

            //Cancel any changes made by user
            initCode();
        }

        //Method to connect to the database
        private void connectToDatabase(String connection_string)
        {
            // Retrieve the connection string
            conString = connection_string;

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //Method to disconnect from database
        private void closeDatabaseConnection()
        {
            //Close database connection
            conn.Close();
        }
    }
}
